<template>
  <div>
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisible"
      width="40%"
      center
      :before-close="handleClose">
      <el-card>
      <el-form :model="userUpdate" status-icon :rules="rules" ref="user" label-width="100px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userUpdate.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="userUpdate.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="handleClose()">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import IdManager from "@/api/dataManage/IdManager";

export default {
  name: "update_user",
  props: {
    userUpdate: Object
  },
  data() {
    return {
      dialogVisible: true,
      user: {
        name: '',
        address: '',
      },
      rules: {
        name: [{required: true, message: '姓名不能为空', trigger: 'blur'}],
        address: [{required: true, message: '地址不能为空', trigger: 'blur'}],
      }
    }
  },
  created() {
  },
  methods: {

    //调用组件初始化事件
    handleClose() {
      this.$emit("close", {name: 'update', isRefresh: false});
    },

    //提交
    submitForm() {
      alert("11")
      this.$refs['user'].validate((valid) => {
        if (valid) {
          const loading = this._loading();
          const api = new IdManager();
          console.log(this.userUpdate)
          api.update(this.userUpdate).then(res => {
            loading.close();
              this.$emit("close", {name: 'update', isRefresh: true});
          }).catch(e => loading.close())
        }
      })

    },

    /*//重置
    resetForm() {
      this.$refs["user"].resetFields();
      this.userUpdate.name = '';
      this.userUpdate.address = '';
    },*/


    //加载提示
    _loading() {
      return this.$loading({
        lock: true,
        text: '请耐心等待.....',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    }
  }

}

</script>

<style scoped>

</style>
